﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Dynamic;
using System.Transactions;
using PagedList;
using Sovigaz.DataProvider;
using Utilities;

namespace Sovigaz.Permission.Repositories
{
    public class RoleRepository : GenericRepository
    {
        public RoleRepository()
            : base("RoleID")
        {
        }

        public ICollection<Role> Roles { get; set; }

        public void Delete(int id)
        {
            var role = GetByID(id);
            if (role != null)
                role.IsDeleted = true;

            _context.SaveChanges();
        }

        public void Delete(string[] idArr)
        {
            foreach (var id in idArr)
            {
                var role = GetByID(ConvertUtility.IntParse(id));
                if (role != null)
                    role.IsDeleted = true;
            }

            _context.SaveChanges();
        }

        public int Update(Role role)
        {
            var updateRole = new Role();

            using (TransactionScope scope = new TransactionScope())
            {
                if (role.RoleID > 0)
                {
                    updateRole = GetByID(role.RoleID);
                    if (!Utility.ArrayEqual(updateRole.Tstamp, role.Tstamp))
                        throw ExceptionHelper.CreateHttpResponseException("Dữ liệu đã bị cập nhật bởi người sử dụng khác.");
                }

                updateRole.RoleID = role.RoleID;
                updateRole.RoleName = role.RoleName;
                updateRole.Description = role.Description;
                updateRole.IsActived = role.IsActived;

                if (updateRole.RoleID == 0)
                    _context.Roles.Add(updateRole);
                else
                {
                    _context.Database.SqlQuery<RolePermission>("sp_RolePermission_Delete @RoleID"
                        , new SqlParameter { ParameterName = "RoleID", Value = updateRole.RoleID }).FirstOrDefault();
                }

                foreach (var rp in role.RolePermissions)
                {
                    rp.RoleID = updateRole.RoleID;
                    _context.RolePermissions.Add(rp);
                }

                _context.SaveChanges();

                scope.Complete();
            }

            _context.Entry(updateRole).Reload();

            return updateRole.RoleID;
        }

        public Role GetByID(int id)
        {
            return _context.Roles.FirstOrDefault(p => p.RoleID == id && !p.IsDeleted);
        }

        public List<Role> GetList()
        {
            return _context.Roles.Where(p => !p.IsDeleted).OrderBy(p => p.RoleName).ToList(); ;
        }

        public RoleRepository GetList(string whereClause, string orderBy, string orderDirection, int pageNumber)
        {
            var roles = _context.Roles.Where(whereClause)
                .OrderBy(String.Format("{0} {1}", orderBy.Trim(), orderDirection.Trim()))
                .ToPagedList(pageNumber, DefaultPageSize);

            TotalItemCount = roles.TotalItemCount;
            TotalItemOnPage = roles.Count;
            PageCount = roles.PageCount;
            PageNumber = roles.PageNumber;
            Roles = roles.ToList();

            return this;
        }
    }
}
